﻿/*****************************************************************//**
  * @file   VAOneXMLAdaptorJunctions.h
  * @brief  VAOne读写交点适配器
  *
  * @author lilongyuan (lilongyuan@diso.cn)
  * @date   2025-01-23
  *********************************************************************/
#ifndef VAONEXMLADAPTORJUNCTIONS_H
#define VAONEXMLADAPTORJUNCTIONS_H

#include "FITKAbstractVAOneXMLAdaptor.h"

namespace IO
{
    /**
     * @brief  XML读写适配器，用于读写交点/耦合数据
     * @author lilongyuan (lilongyuan@diso.cn)
     * @date   2025-01-22
     */
    class FITKVAOneXMLIOAPI VAOneXMLAdaptorJunctions : public FITKAbstractVAOneXMLAdaptor
    {
        FITKCLASS(IO, VAOneXMLAdaptorJunctions);
    public:
        /**
         * @brief     读取共有数据模板
         * @param[i]  parentNode 父节点，data 交点数据
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        template <typename T>
        bool readCommonData(rapidxml::xml_node<char>* parentNode, T data)
        {
            if (parentNode == nullptr || data == nullptr) return false;

            bool ok = true;

            // 启用连接状态
            QString enabled = readStrAttribute(parentNode, "enabled", &ok);
            if (ok != true) return false;
            data->setEnabledJunction(enabled.toLower() == "true");

            // manual
            QString manual = readStrAttribute(parentNode, "manual", &ok);

            // 名称 Name
            QString name = getChildElementText(parentNode, "Name");
            // 添加到哈希表里
            ok &= setIdToHash(VAOne::FITKVAOneXMLHash::Node_Junctions, name, data->getDataObjectID());
            // 设置名称
            data->setDataObjectName(getJunctionManager()->checkName(name));

            // 注释 Note
            QString note = getChildElementText(parentNode, "Note");
            //data->srtNotes();

            return ok;
        }

        /**
         * @brief   构造函数
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        explicit VAOneXMLAdaptorJunctions() = default;

        /**
         * @brief   析构函数
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        ~VAOneXMLAdaptorJunctions() = default;

        /**
         * @brief   适配器读数据
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool adaptR() override;

        /**
         * @brief   适配器写数据
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool adaptW() override;

    private:
        /**
         * @brief     读取点结数据
         * @param[i]  node PointJunction节点
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool readPointJunction(rapidxml::xml_node<char>* node);

        /**
         * @brief     读取线结数据
         * @param[i]  node LineJunction节点
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool readLineJunction(rapidxml::xml_node<char>* node);

        /**
         * @brief     读取区结数据
         * @param[i]  node AreaJunction节点
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool readAreaJunction(rapidxml::xml_node<char>* node);

        /**
         * @brief     写出点结数据
         * @param[i]  node Junctions节点
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool writePointJunction(rapidxml::xml_node<char>* node);

        /**
         * @brief     写出线结数据
         * @param[i]  node Junctions节点
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool writeLineJunction(rapidxml::xml_node<char>* node);

        /**
         * @brief     写出区结数据
         * @param[i]  node Junctions节点
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool writeAreaJunction(rapidxml::xml_node<char>* node);

    private:
        /**
         * @brief     写出共有属性数据
         * @param[i]  parentNode 父节点，enabled 启用状态，manual
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool writeCommonAttribute(rapidxml::xml_node<char>* parentNode, bool enabled, bool manual);

        /**
         * @brief     写出名称和注释数据
         * @param[i]  parentNode 父节点，name 名称，note 注释内容
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool writeNameAndNote(rapidxml::xml_node<char>* parentNode, QString name, QString note);

        /**
         * @brief     写出NodeRef节点数据
         * @param[i]  parentNode 父节点，pointId 点ID
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool writeNodeRef(rapidxml::xml_node<char>* parentNode, int pointId);

        /**
         * @brief     写出FaceRef节点数据
         * @param[i]  parentNode 父节点，faceId 面ID，faceName 面名称
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-01-23
         */
        bool writeFaceRef(rapidxml::xml_node<char>* parentNode, int faceId, QString faceName);
    };
    Register2FITKIOAdaptorFactory(VAOneXML, Junctions, VAOneXMLAdaptorJunctions)
}

#endif // VAONEXMLADAPTORJUNCTIONS_H

